package com.example.springboot_zc.Utils;


import com.example.springboot_zc.mapper.classMapper;
import com.example.springboot_zc.service.classService;
import org.apache.poi.ss.usermodel.*;
import com.example.springboot_zc.pojo.Class;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Component
public class ExcelUtils {
    @Autowired
    classService classService;
    @Autowired
    classMapper classMapper;

    public List<Class> excelToShopIdList(InputStream inputStream) throws IOException {

        Workbook workbook = WorkbookFactory.create(inputStream);
        inputStream.close();
        // 在工作簿获取目标工作表
        Sheet sheet = workbook.getSheetAt(0);
        // 获取到最后一行
        int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
        // 该集合用来储存行对象
        ArrayList<Class> cs = new ArrayList<>();
        // 遍历整张表，从第二行开始，第一行的表头不要，循环次数不大于最后一行的值
        for (int i = 1; i < physicalNumberOfRows; i++) {
            // 该对象用来储存行数据
            Class c = new Class();
            // 获取当前行数据
            Row row = sheet.getRow(i);
            // 获取目标单元格的值并存进对象中
            String cellValue0=new DataFormatter().formatCellValue(row.getCell(0));
            String cellValue1=new DataFormatter().formatCellValue(row.getCell(1));
            String cellValue2=new DataFormatter().formatCellValue(row.getCell(2));
            String cellValue3=new DataFormatter().formatCellValue(row.getCell(3));
            String cellValue4=new DataFormatter().formatCellValue(row.getCell(4));
            String cellValue5=new DataFormatter().formatCellValue(row.getCell(5));
            String cellValue6=new DataFormatter().formatCellValue(row.getCell(6));
            String cellValue7=new DataFormatter().formatCellValue(row.getCell(7));
            String cellValue8=new DataFormatter().formatCellValue(row.getCell(8));
            String cellValue9=new DataFormatter().formatCellValue(row.getCell(9));
//            c.setIndexx(Integer.valueOf(cellValue0));
            c.setStuNum(cellValue1);
            c.setName(cellValue2);
            c.setContent(cellValue3);
            c.setNewTime(cellValue4);
//            c.setSelfScore(Integer.valueOf(cellValue5));
//            c.setClassScore(Integer.valueOf(cellValue6));
            c.setStatus(cellValue7);
            c.setPerson(cellValue8);
            c.setDate(cellValue9);
            // 把对象放到集合里
            cs.add(c);
            System.out.println(classService);
            classService.save(c);
            System.out.println("获取到的当前行数据：" + c);
        }
        System.out.println(cs);
        return cs;
    }
}
